﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;

using ABCat.Core;
using ABCat.Core.Editors;
using ABCat.Core.Plugins;
using ABCat.Shared.Properties;

namespace ABCat.Plugins.DataSources.AudioBooks
{
	[UsedImplicitly]
	[DisplayName("База данных аудиокниг")]
	public sealed class AudioBooksDbContainerSQLiteConfig : Config
	{
		#region + Fields +

		public static readonly object LockContext = new object();

		private string _databaseFolder;

		#endregion
		#region + Properties +

		[Browsable(false)]
		public string AudioBooksFileName
		{
			get { return "AudioBooks.sqlite"; }
		}

		[Browsable(false)]
		public string AudioBooksFilePath
		{
			get { return Path.Combine(DatabaseFolder, AudioBooksFileName); }
		}

		[Browsable(false)]
		public string BinaryDataFileName
		{
			get { return "BinaryData.sqlite"; }
		}

		[Browsable(false)]
		public string BinaryDataFilePath
		{
			get { return Path.Combine(DatabaseFolder, BinaryDataFileName); }
		}

		[DisplayName("Папка БД аудиокниг")]
		[Description("Папка для хранения БД аудиокниг")]
		[Editor(typeof(FolderPathEditor), typeof(FolderPathEditor))]
		public string DatabaseFolder
		{
			get { return _databaseFolder; }
			set
			{
				if (Equals(value, _databaseFolder)) return;
				_databaseFolder = value;
				OnPropertyChanged();
			}
		}

		public override string DisplayName
		{
			get { return "База данных аудиокниг"; }
		}

		[Browsable(false)]
		public string ProcessingSettingsFileName
		{
			get { return "ProcessingSettings.sqlite"; }
		}

		[Browsable(false)]
		public string ProcessingSettingsFilePath
		{
			get { return Path.Combine(DatabaseFolder, ProcessingSettingsFileName); }
		}

		[Browsable(false)]
		public string UserDataFileName
		{
			get { return "UserData.sqlite"; }
		}

		[Browsable(false)]
		public string UserDataFilePath
		{
			get { return Path.Combine(DatabaseFolder, UserDataFileName); }
		}

		[Browsable(false)]
		public DateTime LastAudioBooksVacuum { get; set; }

		#endregion
		#region + Ctor +

		public AudioBooksDbContainerSQLiteConfig()
		{
		}

		#endregion
		#region + Logic +

		public override bool Check(bool correct)
		{
			bool result = true;

			lock (LockContext)
			{
				if (DatabaseFolder.IsNullOrEmpty())
				{
					result = false;
					DatabaseFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ABCat", "DataBases");
				}

				if (LastAudioBooksVacuum == DateTime.MinValue) LastAudioBooksVacuum = DateTime.Now;

				if (correct)
				{
					Directory.CreateDirectory(DatabaseFolder);
					if (!File.Exists(AudioBooksFilePath) && File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DB", AudioBooksFileName)))
					{
						File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DB", AudioBooksFileName), AudioBooksFilePath);
					}
					if (!File.Exists(BinaryDataFilePath) && File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DB", BinaryDataFileName)))
					{
						File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DB", BinaryDataFileName), BinaryDataFilePath);
					}
				}
			}

			return result;
		}

		#endregion
	}
}